package ArrayList;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class SafeListDemo {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> list = new CopyOnWriteArrayList<>();

        Runnable task = () -> {
            for (int i = 0; i < 1000; i++) {
                list.add(i); // 线程安全的添加
            }
        };

        Thread t1 = new Thread(task);
        Thread t2 = new Thread(task);
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("CopyOnWriteArrayList 大小应为 2000，实际为: " + list.size());
    }
}
